﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Mvc;
using GWEB.WebBase.Data;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;
using Newtonsoft.Json;

namespace GWEB.WebFrame.Account
{
    public class BindListController : Controller
    {
        [ActionFilter(IsLogin = true)]
        public ActionResult Index(String Id,String KeyCode,String ClassName,String Cid)
        {
         
            ViewBag.ClassName = ClassName;
            ViewBag.Id = Id;        
            ViewBag.Cid = Cid;
            return View(BasePage.V("Account/BindList"));            
        }
      
        [ActionFilter(IsLogin = true)]
        public JsonResult Data(String Id, String KeyCode,String ClassName)
        {
            DataRow drUser = BasePage.GetUser(Session);
            String leagueId = Fun.cString(drUser["LeagueId"].ToString());

            String sql = "";

            if(ClassName=="04")
          sql = @"select cu_Cus_followup.id,CustName,CustTel,LinkName,Sex,convert(varchar(10),BirdDate,120) BirdDate ,convert(varchar(10),MakeDate,120) MakeDate,AdviserId,sshowname,cu_Cus_followup.CourseId,CouTeachId,ClassroomName,CourseName,ClassName,TeacherName from cu_Cus_followup
left join 
cu_Customers on cu_Customers.id=cu_Cus_followup.Cid 
left join 
tb_sys_users on tb_sys_users.iuserid=cu_Cus_followup.AdviserId
left join 
cu_TeacherCourse on cu_TeacherCourse.id=cu_Cus_followup.CouTeachId
where IsAuditions='1' and isnull(IsCharge,0)=0   and Cid={0} ";      
          if(ClassName=="02")
                sql = @"select cu_Contract.id,CustName,CustTel,LinkName,Sex,BirdDate,contractNumber,SetmealName,cu_Contract.AchievementId AdviserId,sshowname,cu_Contract.DisPrice,cu_Contract.ContractPrice,isnull(ContractJe,0) ContractJe,isnull(DisPrice-ContractJe,0) ce from cu_Contract
left join 
tb_sys_users on tb_sys_users.iuserid=cu_Contract.AchievementId
left join 
cu_Customers on cu_Customers.id=cu_Contract.Cid 
left join 
(select sum(Account) ContractJe, contractid from cu_Account where state<=3 group by contractid) Ht on Ht.ContractId=cu_Contract.id 
where status in (0,1,2,3,4) and DisPrice>isnull(ContractJe,0) and Cid={0} ";
            if (ClassName == "03")
                sql = @"select ChargeName,ActivityName,convert(varchar(10),MPrice)+'/'+convert(varchar(10),NoMPrice) MPrice,Classhour,convert(varchar(10),EndSign,120) EndSign,convert(varchar(10),Actstart,120)+'/'+CONVERT(varchar(10),Actend,120) ActDate,convert(varchar(5),MonthStart)+'-'+CONVERT(varchar(5),MonthEnd) Ylfw,cu_Activity_SignUp.id,Cid,Name,cu_Activity_SignUp.Tel,LinkName,convert(varchar(10),subscribeDate,120) subscribeDate from cu_Activity_SignUp
left join cu_Activity on cu_Activity.id=cu_Activity_SignUp.ActivityId
where 
cu_Activity.state =1 and cu_Activity_SignUp.state in (0,1,2) and Cid={0}";
            BasePage.bd.ColumnNameToLower = false;
            String str = JsonConvert.SerializeObject(BasePage.bd.ExecuteTable(string.Format(sql,Id)));

            BasePage.bd.ColumnNameToLower = true;
            List<Dictionary<String, Object>> data = JsonConvert.DeserializeObject<List<Dictionary<String, Object>>>(str);
            return BasePage.Json(ResultData.OK(data));

        }
        [ActionFilter(IsLogin = true)]
        public JsonResult UpdateState(String id, String keyCode, String AccountId,String ClassName)
        {
            //if (!Fun.IDVerify(id, keyCode))
            //    return BasePage.Json(ResultData.Code("0001", "您没有权限"));

            BaseData bd = new BaseData();
            string UserId = BasePage.GetUserId(Session);
          
            String[] ExeSql = new String[4];
            
                switch (ClassName)
                { 
                    case "02":
                    string sql = @"select cu_account.cid,htssje,djsyje ,DisPrice from cu_account 
                        left join 
                        (select  SUM(case when FlowingType in ('00101001', '00201001')  and state = 3 then Account else 0 end ) djsyje,SUM(case when FlowingType in ('00102001', '00202001')  and state = 2 then Account else 0 end ) htssje   from cu_Account where ContractId="+id+@" ) sydj
  left join
  cu_Contract on cu_Contract.id = "+id+@"
   where cu_account.id =" + AccountId;
                    DataTable dt = BasePage.bd.ExecuteTable(string.Format(sql, AccountId));
                    ExeSql[0] = " update cu_Account set ContractId = " + id + " where id =" + AccountId;
                    decimal htssje = 0;
                    decimal djje = 0;
                    decimal htje = 0;

                    try
                    {
                        htssje = decimal.Parse(dt.Rows[0]["htssje"].ToString());//已收合同
                        djje = decimal.Parse(dt.Rows[0]["djsyje"].ToString());//订金金额
                        htje = decimal.Parse(dt.Rows[0]["DisPrice"].ToString());//合同金额

                    }
                    catch { }
                    if (htssje + djje >= htje)
                    {
                        ExeSql[1] = "update cu_Contract set status=2  where id=" + id;
                        ExeSql[2] = "update cu_Customers set CustState=6  where id=" + dt.Rows[0]["cid"].ToString();
                    }
                    break;
                    case "03":
                    ExeSql[0] = " update cu_Account set activityId = " + id + " where id =" + AccountId;
                    ExeSql[1] = "update cu_Activity_SignUp set state=3  where  Id= " + id;
                    break;
                    case "04":
                    ExeSql[0] = " update cu_Account set FollowUPId = "+id+" where id ="+AccountId;
                    ExeSql[1] = "update cu_Cus_followup set IsCharge = 1, ChargeId ="+AccountId+" where id = "+id;
                        break;
                   
                }
          
            

            bool t = bd.ExecuteSQL(ExeSql);
            if (t)
                return BasePage.Json(ResultData.Code("0000", WebMessage.SaveSuccess));
            else
                return BasePage.Json(ResultData.Code("0001", WebMessage.SaveFailure));
        }

    }
}